package com.infore.dataReceiving.outsideInterface.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.infore.dataReceiving.constant.Constants;
import com.infore.dataReceiving.entity.HourData;
import com.infore.dataReceiving.enums.ResultStatus;
import com.infore.dataReceiving.outsideInterface.service.IHourDataService;
import com.infore.dataReceiving.repository.IHourDataRepository;
import com.infore.dataReceiving.repository.IRealTimeDataRepository;
import com.infore.dataReceiving.util.ArrayUtils;
import com.infore.dataReceiving.util.DateUtil;
import com.infore.dataReceiving.util.ResultEntity;

@Service
public class HourDataServiceImpl implements IHourDataService{

	@Autowired
	private IHourDataRepository iHourDataRepository;
	@Autowired
	private IRealTimeDataRepository iRealTimeDataRepository;
	
	@SuppressWarnings("serial")
	public ResultEntity queryHourDatas(String stationCode, String factorCode, String startTime, String endTime) {
		List<HourData> list = 
		iHourDataRepository.findAll(new Specification<HourData>() {
			public Predicate toPredicate(Root<HourData> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(stationCode)) {
					List<String> stationCodeList =  ArrayUtils.stringToList(stationCode);
					predicates.add(criteriaBuilder.in(root.get("mn")).value(stationCodeList));
				}
				if (StringUtils.isNotBlank(factorCode)) {
					List<String> factorCodeList =  ArrayUtils.stringToList(factorCode);
					predicates.add(criteriaBuilder.in(root.get("factorCode")).value(factorCodeList));
				}
				if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
					Date startDate = DateUtil.stringToDate(startTime, DateUtil.DATE_FULL_STR);
					Date endDate = DateUtil.stringToDate(endTime, DateUtil.DATE_FULL_STR);
					predicates.add(criteriaBuilder.between(root.get("dataTime"),startDate, endDate));
				}
				return query.where(predicates.toArray(new Predicate[predicates.size()]))
						.orderBy(criteriaBuilder.asc(root.get("dataTime"))).getRestriction();
			}
		});
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}

	public ResultEntity audioSingReport(String stationCode, String factorCode, String startTime, String endTime) {
		List<String> mnList =  ArrayUtils.stringToList(stationCode);
		if (StringUtils.isEmpty(factorCode)) {
			return new ResultEntity(ResultStatus.ERROR,Constants.QUERY_SUCCESS);
		}
		List<String> projectCodes =  ArrayUtils.stringToList(factorCode);
		List<Map<String, Object>> list = iHourDataRepository.audioSingReport(startTime, endTime, projectCodes, mnList);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}

	public ResultEntity audioMoreReport(String stationCode, String factorCode, String startTime, String endTime) {
		List<String> mnList =  ArrayUtils.stringToList(stationCode);
		if (StringUtils.isEmpty(factorCode)) {
			return new ResultEntity(ResultStatus.ERROR,Constants.QUERY_SUCCESS);
		}
		List<String> projectCodes =  ArrayUtils.stringToList(factorCode);
		List<Map<String, Object>> list = iHourDataRepository.audioMoreReport(startTime, endTime, projectCodes, mnList);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}

	public ResultEntity realDataGroup(String stationCode, String factorCode, String startTime, String endTime) {
		List<String> mnList =  ArrayUtils.stringToList(stationCode);
		if (StringUtils.isEmpty(factorCode)) {
			return new ResultEntity(ResultStatus.ERROR,Constants.QUERY_SUCCESS);
		}
		List<String> projectCodes =  ArrayUtils.stringToList(factorCode);
		List<Map<String, Object>> list = iRealTimeDataRepository.queryGroupByMnCode(startTime, endTime, projectCodes, mnList);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}

	public ResultEntity realData(String stationCode, String factorCode, String startTime, String endTime) {
		List<String> mnList =  ArrayUtils.stringToList(stationCode);
		if (StringUtils.isEmpty(factorCode)) {
			return new ResultEntity(ResultStatus.ERROR,Constants.QUERY_SUCCESS);
		}
		List<String> projectCodes =  ArrayUtils.stringToList(factorCode);
		List<Map<String, Object>> list = iRealTimeDataRepository.queryByMnCode(startTime, endTime, projectCodes, mnList);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}
	
	@Override
    public ResultEntity getDayData() {
        // 先获取未统计的日时间集合
        List<Map<String, Object>> parDayList = iHourDataRepository.getUnStatisticsDayDataTime();
        List<String> dayTimeList = parDayList.stream().map(l -> l.get("TIME").toString()).collect(Collectors.toList());
        List<Map<String, Object>> dayDataList = new ArrayList<Map<String, Object>>();
        if (dayTimeList.size() > 0) {
            dayDataList = iHourDataRepository.getCountDayData(dayTimeList);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(dayDataList);
    }

    @Override
    public ResultEntity updateFlag() {
        int updateHourNum = iHourDataRepository.updateHourDayData(1);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setMapValue("updateHourNum", updateHourNum);
    }
    
    @Override
    public ResultEntity getUnStatisticsWeekDataTime() {
        List<Map<String, Object>> list = iHourDataRepository.getUnStatisticsWeekDataTime();
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity getUnStatisticsMonthDataTime() {
        List<Map<String, Object>> list = iHourDataRepository.getUnStatisticsMonthDataTime();
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity getUnStatisticsQuarterDataTime() {
        List<Map<String, Object>> list = iHourDataRepository.getUnStatisticsQuarterDataTime();
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity getUnStatisticsYearDataTime() {
        List<Map<String, Object>> list = iHourDataRepository.getUnStatisticsYearDataTime();
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }
    
}
